<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    var nums1 = [1, 2, 3, 0, 0, 0]
    var m = 3
    var nums2 = [2, 5, 6]
    var n = 3
    // 直接合并后排序
    var merge = function (nums1, m, nums2, n) {
        nums1.splice(m, nums1.length - m, ...nums2);
        nums1.sort((a, b) => a - b);
    };
    // 双指针
    var merge2 = function (nums1, m, nums2, n) {
        let p1 = 0, p2 = 0;
        const sorted = new Array(m + n).fill(0);
        var cur;
        while (p1 < m || p2 < n) {
            if (p1 === m) {
                cur = nums2[p2++];
            } else if (p2 === n) {
                cur = nums1[p1++];
            } else if (nums1[p1] < nums2[p2]) {
                cur = nums1[p1++];
            } else {
                cur = nums2[p2++];
            }
            sorted[p1 + p2 - 1] = cur;
        }
        for (let i = 0; i != m + n; ++i) {
            nums1[i] = sorted[i];
        }
    };
    // 逆向双指针
    var merg3 = function (nums1, m, nums2, n) {
        let p1 = m - 1
        let p2 = n - 1
        let length = m + n - 1
        while (p1 >= 0 || p2 >= 0) {
            if (p1 === -1) {
                nums1[length] = nums2[p2]
                p2--
                length--
            } else if (p2 === -1) {
                nums1[length] = nums1[p1]
                p1--
                length--
            }else if (nums1[p1] > nums2[p2]) {
                nums1[length] = nums1[p1]
                p1--
                length--
            } else {
                nums1[length] = nums2[p2]
                p2--
                length--
            }
        }
    };
    merge3(nums1, m, nums2, n)
    console.log(nums1);
</script>

</html>